/*
 * Pythonforc - A lightweight Python interpreter in C
 * Copyright (C) 2025 Pythonforc Developers
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef PYTHONFORC_H
#define PYTHONFORC_H

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* 基本类型定义 */
typedef int64_t py_int;
typedef double py_float;
typedef unsigned char py_byte;

/* 对象系统 */
typedef enum {
    PY_TYPE_NONE,
    PY_TYPE_INT,
    PY_TYPE_FLOAT,
    PY_TYPE_BOOL,
    PY_TYPE_STRING,
    PY_TYPE_BYTES,
    PY_TYPE_LIST,
    PY_TYPE_TUPLE,
    PY_TYPE_DICT,
    PY_TYPE_FUNCTION,
    PY_TYPE_NATIVE_FUNCTION,
    PY_TYPE_MODULE,
    PY_TYPE_TYPE,
    PY_TYPE_EXCEPTION
} py_type;

typedef struct py_object {
    py_type type;
    int ref_count;
    struct py_object* next; /* GC链表 */
} py_object;

typedef struct {
    py_object obj;
    py_int value;
} py_int_object;

typedef struct {
    py_object obj;
    py_float value;
} py_float_object;

typedef struct {
    py_object obj;
    bool value;
} py_bool_object;

typedef struct {
    py_object obj;
    size_t length;
    char* value;
} py_string_object;

typedef struct {
    py_object obj;
    size_t length;
    py_byte* value;
} py_bytes_object;

typedef struct {
    py_object obj;
    size_t length;
    size_t capacity;
    py_object** items;
} py_list_object;

typedef struct {
    py_object obj;
    size_t length;
    py_object** items;
} py_tuple_object;

typedef struct dict_entry {
    py_object* key;
    py_object* value;
    struct dict_entry* next;
} dict_entry;

typedef struct {
    py_object obj;
    size_t count;
    size_t capacity;
    dict_entry** buckets;
} py_dict_object;

/* 前向声明 */
struct py_code_object;

typedef struct {
    py_object obj;
    char* name;
    py_object* globals;
    struct py_code_object* code;
} py_function_object;

typedef py_object* (*py_native_function)(py_object* self, py_object* args);

typedef struct {
    py_object obj;
    py_native_function function;
    char* name;
} py_native_function_object;

typedef struct {
    py_object obj;
    char* name;
    py_dict_object* dict;
} py_module_object;

typedef struct {
    py_object obj;
    char* name;
    py_type base_type;
    py_dict_object* dict;
} py_type_object;

typedef struct {
    py_object obj;
    char* name;
    py_object* value;
} py_exception_object;

/* 字节码相关 */
typedef enum {
    OP_LOAD_CONST,
    OP_LOAD_NAME,
    OP_STORE_NAME,
    OP_DELETE_NAME,
    OP_LOAD_ATTR,
    OP_STORE_ATTR,
    OP_DELETE_ATTR,
    OP_LOAD_GLOBAL,
    OP_STORE_GLOBAL,
    OP_DELETE_GLOBAL,
    OP_LOAD_FAST,
    OP_STORE_FAST,
    OP_DELETE_FAST,
    OP_LOAD_CLOSURE,
    OP_LOAD_DEREF,
    OP_STORE_DEREF,
    OP_DELETE_DEREF,
    OP_CALL_FUNCTION,
    OP_CALL_METHOD,
    OP_RETURN_VALUE,
    OP_JUMP_FORWARD,
    OP_JUMP_IF_FALSE,
    OP_JUMP_IF_TRUE,
    OP_JUMP_ABSOLUTE,
    OP_POP_JUMP_IF_FALSE,
    OP_POP_JUMP_IF_TRUE,
    OP_FOR_ITER,
    OP_BREAK_LOOP,
    OP_CONTINUE_LOOP,
    OP_SETUP_LOOP,
    OP_SETUP_EXCEPT,
    OP_SETUP_FINALLY,
    OP_POP_BLOCK,
    OP_RAISE_VARARGS,
    OP_MAKE_FUNCTION,
    OP_BUILD_LIST,
    OP_BUILD_TUPLE,
    OP_BUILD_DICT,
    OP_BUILD_SET,
    OP_BUILD_SLICE,
    OP_UNPACK_SEQUENCE,
    OP_UNPACK_EX,
    OP_BINARY_ADD,
    OP_BINARY_SUBTRACT,
    OP_BINARY_MULTIPLY,
    OP_BINARY_DIVIDE,
    OP_BINARY_FLOOR_DIVIDE,
    OP_BINARY_MODULO,
    OP_BINARY_POWER,
    OP_BINARY_LSHIFT,
    OP_BINARY_RSHIFT,
    OP_BINARY_AND,
    OP_BINARY_OR,
    OP_BINARY_XOR,
    OP_INPLACE_ADD,
    OP_INPLACE_SUBTRACT,
    OP_INPLACE_MULTIPLY,
    OP_INPLACE_DIVIDE,
    OP_INPLACE_FLOOR_DIVIDE,
    OP_INPLACE_MODULO,
    OP_INPLACE_POWER,
    OP_INPLACE_LSHIFT,
    OP_INPLACE_RSHIFT,
    OP_INPLACE_AND,
    OP_INPLACE_OR,
    OP_INPLACE_XOR,
    OP_UNARY_POSITIVE,
    OP_UNARY_NEGATIVE,
    OP_UNARY_NOT,
    OP_UNARY_INVERT,
    OP_COMPARE_OP,
    OP_GET_ITER,
    OP_YIELD_VALUE,
    OP_IMPORT_NAME,
    OP_IMPORT_FROM,
    OP_IMPORT_STAR,
    OP_LOAD_BUILD_CLASS,
    OP_SETUP_WITH,
    OP_WITH_CLEANUP,
    OP_POP_TOP,
    OP_ROT_TWO,
    OP_ROT_THREE,
    OP_ROT_FOUR,
    OP_DUP_TOP,
    OP_DUP_TOP_TWO,
    OP_NOP
} opcode;

typedef struct py_code_object {
    py_object obj;
    int arg_count;
    int local_count;
    int stack_size;
    int flags;
    char* name;
    char* filename;
    int first_lineno;
    py_object* consts;
    py_object* names;
    py_object* locals;
    py_object* freevars;
    py_object* cellvars;
    unsigned char* code;
    int code_length;
    py_object* lnotab;
} py_code_object;

/* 虚拟机状态 */
typedef struct py_frame {
    py_object** stack;
    int stack_size;
    int stack_pointer;
    py_object** locals;
    py_object* globals;
    py_object* builtins;
    py_object* exception;
    py_object* traceback;
    int last_instruction;
    py_code_object* code;
    struct py_frame* previous;
} py_frame;

typedef struct {
    py_object* modules;
    py_object* builtins;
    py_frame* current_frame;
    bool gc_enabled;
    py_object* gc_head;
    size_t gc_threshold;
    size_t gc_count;
} py_vm;

/* API函数 */
py_vm* py_vm_new(void);
void py_vm_free(py_vm* vm);
py_object* py_vm_run(py_vm* vm, py_code_object* code);
void py_vm_register_module(py_vm* vm, const char* name, py_object* module);
void py_vm_register_builtin(py_vm* vm, const char* name, py_native_function func);

/* 对象创建函数 */
py_object* py_none_new(void);
py_object* py_int_new(py_int value);
py_object* py_float_new(py_float value);
py_object* py_bool_new(bool value);
py_object* py_string_new(const char* value);
py_object* py_bytes_new(const py_byte* value, size_t length);
py_object* py_list_new(size_t capacity);
py_object* py_tuple_new(size_t length);
py_object* py_dict_new(size_t capacity);
py_object* py_function_new(py_code_object* code, py_object* globals, const char* name);
py_object* py_native_function_new(py_native_function func, const char* name);
py_object* py_module_new(const char* name);
py_object* py_type_new(const char* name, py_type base_type);
py_object* py_exception_new(const char* name, py_object* value);

/* 对象操作函数 */
void py_object_incref(py_object* obj);
void py_object_decref(py_object* obj);
py_object* py_object_getattr(py_object* obj, const char* name);
void py_object_setattr(py_object* obj, const char* name, py_object* value);
void py_object_delattr(py_object* obj, const char* name);
py_object* py_object_call(py_object* func, py_object* args);
py_object* py_object_str(py_object* obj);
py_object* py_object_repr(py_object* obj);
py_object* py_object_add(py_object* left, py_object* right);
py_object* py_object_sub(py_object* left, py_object* right);
py_object* py_object_mul(py_object* left, py_object* right);
py_object* py_object_div(py_object* left, py_object* right);
py_object* py_object_compare(py_object* left, py_object* right, int op);

/* 字典操作 */
void py_dict_set_item(py_dict_object* dict, py_object* key, py_object* value);
py_object* py_dict_get_item(py_dict_object* dict, py_object* key);
void py_dict_del_item(py_dict_object* dict, py_object* key);

/* 列表操作 */
void py_list_append(py_list_object* list, py_object* item);
py_object* py_list_get_item(py_list_object* list, size_t index);
void py_list_set_item(py_list_object* list, size_t index, py_object* item);

/* 垃圾回收 */
void py_gc_enable(py_vm* vm, bool enable);
void py_gc_collect(py_vm* vm);
void py_gc_set_threshold(py_vm* vm, size_t threshold);

/* 内置函数 */
py_object* py_builtin_print(py_object* self, py_object* args);
py_object* py_builtin_len(py_object* self, py_object* args);
py_object* py_builtin_range(py_object* self, py_object* args);
py_object* py_builtin_input(py_object* self, py_object* args);
py_object* py_builtin_open(py_object* self, py_object* args);
py_object* py_builtin_type(py_object* self, py_object* args);
py_object* py_builtin_isinstance(py_object* self, py_object* args);
py_object* py_builtin_issubclass(py_object* self, py_object* args);
py_object* py_builtin_hasattr(py_object* self, py_object* args);
py_object* py_builtin_getattr(py_object* self, py_object* args);
py_object* py_builtin_setattr(py_object* self, py_object* args);
py_object* py_builtin_delattr(py_object* self, py_object* args);
py_object* py_builtin_dir(py_object* self, py_object* args);
py_object* py_builtin_abs(py_object* self, py_object* args);
py_object* py_builtin_max(py_object* self, py_object* args);
py_object* py_builtin_min(py_object* self, py_object* args);
py_object* py_builtin_sum(py_object* self, py_object* args);

/* 类型方法 */
py_object* py_list_append_method(py_object* self, py_object* args);
py_object* py_list_pop_method(py_object* self, py_object* args);
py_object* py_list_count_method(py_object* self, py_object* args);
py_object* py_string_lower_method(py_object* self, py_object* args);
py_object* py_string_upper_method(py_object* self, py_object* args);
py_object* py_string_split_method(py_object* self, py_object* args);

/* 编译器 */
py_code_object* py_compile(const char* source, const char* filename, int mode);

/* 导入支持 */
py_object* py_import_module(py_vm* vm, const char* name);

#endif /* PYTHONFORC_H */